package multi_thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @description:
 * @author: walt-zhong
 * @createDate: 2021/3/8
 */
public class printABC{
    private volatile int state = 0;
   public class print implements Runnable{
       private int printCount = 10;
       Object object;
       private int thisFlag;
       private int nextFlag;
       private char printStr;

       public print(Object object,int thisFlag,int nextFlag,char printStr){
           this.object=object;
           this.thisFlag=thisFlag;
           this.nextFlag=nextFlag;
           this.printStr=printStr;
       }

       @Override
       public void run() {
           synchronized (object){
               for (int i = 0; i < printCount; i++) {
                   while(state != thisFlag){
                       try {
                           object.wait();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
                   System.out.print(printStr);
                   state=nextFlag;
                   object.notifyAll();
               }
           }
       }
   }

    public void test(){
        Object object = new Object();
        print printA = new print(object,0,1,'A');
        print printB = new  print(object,1,2,'B');
        print printC = new  print(object,2,3,'C');
        print printD = new  print(object,3,0,'D');

        try {
            new Thread(printA).start();
            new Thread(printB).start();
            new Thread(printC).start();
            new Thread(printD).start();
        }catch (Exception e){

        }
    }
}
